﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class CompanyDAL : BaseStorageDAL, ICompanyDAL
	{
		#region ICompanyDAL Members

		public void CreateCompany(CustomerCompany company, IDbTransaction transaction)
		{
			if (company == null)
				return;

			//Check if company exists
			dtoXmlStorage.CompanyRow companyRow = Storage.Company.FindById(company.Id);

			bool isNew = (companyRow == null);
			if (isNew)
			{
				companyRow = Storage.Company.NewCompanyRow();
			}

			companyRow.Id = company.Id;

			if (!string.IsNullOrEmpty(company.Name))
				companyRow.Name = company.Name;

			if (!string.IsNullOrEmpty(company.Address))
				companyRow.Address = company.Address;

			if (!string.IsNullOrEmpty(company.City))
				companyRow.City = company.City;

			if (!string.IsNullOrEmpty(company.Country))
				companyRow.Country = company.Country;

			if (!string.IsNullOrEmpty(company.Currency))
				companyRow.Currency = company.Currency;

			if (!string.IsNullOrEmpty(company.Phone))
				companyRow.Phone = company.Phone;

			if (!string.IsNullOrEmpty(company.State))
				companyRow.State = company.State;

			if (!string.IsNullOrEmpty(company.Zip))
				companyRow.Zip = company.Zip;

			if (!string.IsNullOrEmpty(company.CustomerIdentity))
				companyRow.CustomerIdentity = company.CustomerIdentity;

			if (!string.IsNullOrEmpty(company.TPIdentity))
				companyRow.TPIdentity = company.TPIdentity;


			if (!string.IsNullOrEmpty(company.SiteId))
				companyRow.SiteId = company.SiteId;

			if (isNew)
			{
				Storage.Company.AddCompanyRow(companyRow);
			}

			if (company.BillingInfo != null)
			{
				//Save Billing Info
				dtoXmlStorage.BillingInfoRow billingrow = Storage.BillingInfo.FindById(company.BillingInfo.Id);

				isNew = (billingrow == null);

				if (isNew)
				{
					billingrow = Storage.BillingInfo.NewBillingInfoRow();
				}

				BillingInfo bi = company.BillingInfo;

				billingrow.Id = bi.Id;
				billingrow.CompanyId = company.Id;

				if (!string.IsNullOrEmpty(bi.Name))
					billingrow.Name = bi.Name;

				if (!string.IsNullOrEmpty(bi.BillingFirstName))
					billingrow.BillingFirstName = bi.BillingFirstName;

				if (!string.IsNullOrEmpty(bi.BillingLastName))
					billingrow.BillingLastName = bi.BillingLastName;

				if (!string.IsNullOrEmpty(bi.BillingEmail))
					billingrow.BillingEmail = bi.BillingEmail;

				if (!string.IsNullOrEmpty(bi.BillingCountry))
					billingrow.BillingCountry = bi.BillingCountry;

				if (!string.IsNullOrEmpty(bi.BillingStreetAddress))
					billingrow.BillingStreetAddress = bi.BillingStreetAddress;

				if (!string.IsNullOrEmpty(bi.BillingStreetAddress2))
					billingrow.BillingStreetAddress2 = bi.BillingStreetAddress2;

				if (!string.IsNullOrEmpty(bi.BillingPostalCode))
					billingrow.BillingPostalCode = bi.BillingPostalCode;

				if (!string.IsNullOrEmpty(bi.BillingCity))
					billingrow.BillingCity = bi.BillingCity;

				if (!string.IsNullOrEmpty(bi.BillingCurrency))
					billingrow.BillingCurrency = bi.BillingCurrency;


				if (!string.IsNullOrEmpty(bi.CustomerIdentity))
					billingrow.CustomerIdentity = bi.CustomerIdentity;

				if (!string.IsNullOrEmpty(bi.TPIdentity))
					billingrow.TPIdentity = bi.TPIdentity;


				if (isNew)
				{
					Storage.BillingInfo.AddBillingInfoRow(billingrow);
				}

			}

			if (transaction == null)
			{
				AcceptChanges();
			}

		}

		public CustomerCompany ReadCompany(string companyId)
		{
			dtoXmlStorage.CompanyRow companyRow = Storage.Company.FindById(companyId);

			if (companyRow == null)
				return null;

			CustomerCompany company = new CustomerCompany();
			company.Id = companyRow.Id;
			company.Name = companyRow.Name;
			company.Address = companyRow.Address;
			company.City = companyRow.City;
			company.Country = companyRow.Country;
			company.Currency = companyRow.Currency;
			company.Phone = companyRow.Phone;
			company.State = companyRow.State;
			company.Zip = companyRow.Zip;
			company.SiteId = companyRow.SiteId;

			if (!companyRow.IsCustomerIdentityNull())
				company.CustomerIdentity = companyRow.CustomerIdentity;

			if (!companyRow.IsTPIdentityNull())
				companyRow.TPIdentity = companyRow.TPIdentity;


			dtoXmlStorage.BillingInfoRow[] birs = companyRow.GetBillingInfoRows();

			if (birs.Length > 0)
			{
				dtoXmlStorage.BillingInfoRow bir = birs[0];

				BillingInfo billinginfo = new BillingInfo();
				billinginfo.Company = company;
				company.BillingInfo = billinginfo;

				billinginfo.Id = bir.Id;
				if (!bir.IsNameNull())
					billinginfo.Name = bir.Name;
				if (!bir.IsBillingFirstNameNull())
					billinginfo.BillingFirstName = bir.BillingFirstName;
				if (!bir.IsBillingLastNameNull())
					billinginfo.BillingLastName = bir.BillingLastName;
				if (!bir.IsBillingEmailNull())
					billinginfo.BillingEmail = bir.BillingEmail;
				if (!bir.IsBillingCountryNull())
					billinginfo.BillingCountry = bir.BillingCountry;
				if (!bir.IsBillingStreetAddressNull())
					billinginfo.BillingStreetAddress = bir.BillingStreetAddress;
				if (!bir.IsBillingStreetAddress2Null())
					billinginfo.BillingStreetAddress2 = bir.BillingStreetAddress2;
				if (!bir.IsBillingPostalCodeNull())
					billinginfo.BillingPostalCode = bir.BillingPostalCode;
				if (!bir.IsBillingCityNull())
					billinginfo.BillingCity = bir.BillingCity;
				if (!bir.IsBillingCityNull())
					billinginfo.BillingCurrency = bir.BillingCurrency;

				if (!bir.IsCustomerIdentityNull())
					billinginfo.CustomerIdentity = bir.CustomerIdentity;

				if (!bir.IsTPIdentityNull())
					billinginfo.TPIdentity = bir.TPIdentity;

			}

			return company;
		}




		public void UpdateCompany(CustomerCompany company, IDbTransaction transaction)
		{
			CreateCompany(company, transaction);
		}
		#endregion


		public CustomerCompany ReadCompanyBySiteId(string siteId)
		{
			foreach (dtoXmlStorage.CompanyRow cr in Storage.Company)
			{
				if (String.Compare(cr.SiteId, siteId, true) == 0)
					return ReadCompany(cr.Id);
			}

			return null;
		}
	}
}